<template>
  <div>
    <el-form
      ref="formValidate"
      :model="formValidate"
      :rules="ruleValidate"
      label-width="90px"
    >
      <el-form-item label="奖品" prop="type">
        <el-radio-group v-model="formValidate.type">
          <el-radio :label="1">未中奖</el-radio>
          <el-radio :label="5">平台优惠券</el-radio>
          <el-radio :label="10">商家优惠券</el-radio>
          <!--    <el-radio :label="2">积分</el-radio>
          <el-radio :label="6">商品</el-radio>
          <el-radio :label="4">红包</el-radio> -->
          <!-- <el-radio :label="11">幸运分</el-radio> -->
          <el-radio :label="3">余额</el-radio>
        </el-radio-group>
      </el-form-item>
      <!-- 赠送平台券 -->
      <el-form-item label="赠送平台券：" v-if="formValidate.type == 5">
        <div v-if="couponName.length" class="mb20">
          <el-tag
            closable
            v-for="(item, index) in couponName"
            :key="index"
            @close="handleClose(item)"
            >{{ item.title }}</el-tag
          >
        </div>
        <el-button type="primary" @click="addCoupon" v-if="!couponName.length"
          >添加优惠券</el-button
        >
      </el-form-item>
      <!-- 赠送商家券 -->
      <el-form-item label="赠送商家券：" v-if="formValidate.type == 10">
        <div v-if="couponName_mer.length" class="mb20">
          <el-tag
            closable
            v-for="(item, index) in couponName_mer"
            :key="index"
            @close="handleClose_mer(item)"
            >{{ item.title }}</el-tag
          >
        </div>
        <el-button
          type="primary"
          @click="addCoupon_mer"
          v-if="!couponName_mer.length"
          >添加优惠券</el-button
        >
      </el-form-item>

      <el-form-item
        :label="[3, 4].includes(formValidate.type) ? '金额信息' : '积分数量'"
        prop="num"
        v-if="[2, 3, 4].includes(formValidate.type)"
      >
        <el-input-number
          v-model="formValidate.num"
          placeholder="请输入金额数量"
          :max="formValidate.type == 4 ? 999 : 99999"
          :min="1"
          style="width: 300px"
        ></el-input-number>
        <div class="ml100 grey">
          {{
            formValidate.type == 3
              ? "用户领取余额后会自动到账余额账户"
              : formValidate.type == 4
              ? "用户领取红包后会自动到账微信零钱，添加此奖品需开通微信支付,并且账户中金额不能小于1元"
              : ""
          }}
        </div>
      </el-form-item>
      <el-form-item
        v-if="formValidate.type == 6"
        label="商品"
        prop="goods_image"
      >
        <template>
          <div v-if="formValidate.goods_image" class="upload-list">
            <img :src="formValidate.goods_image" />
            <i class="el-icon-delete elicon" @click="removeGoods()" size="50" />
          </div>
          <div v-else class="upLoad pictrueTab acea-row row-center-wrapper">
            <i class="el-icon-picture addicon" @click="addgoods()" size="100" />
          </div>
        </template>
      </el-form-item>
      <el-form-item label="奖品名称" prop="name">
        <el-input
          v-model="formValidate.name"
          :maxlength="10"
          placeholder="请输入奖品名称"
          style="width: 300px"
        />
      </el-form-item>
      <el-form-item label="奖品图片" prop="image">
        <template>
          <div class="upload-list" v-if="formValidate.image">
            <img :src="formValidate.image" />
            <i class="el-icon-delete elicon" size="50" @click="remove()" />
          </div>
          <div v-else class="upLoad pictrueTab acea-row row-center-wrapper">
            <i
              class="el-icon-picture-outline addicon"
              @click="modalPicTap()"
              size="100"
            />
          </div>
        </template>
        <!-- <div class="info">选择商品</div> -->
      </el-form-item>
      <el-form-item label="奖品数量" prop="total">
        <el-input-number
          v-model="formValidate.total"
          placeholder="请输入奖品数量"
          :max="99999"
          :min="0"
          :precision="0"
          style="width: 300px"
        ></el-input-number>
      </el-form-item>
      <el-form-item label="奖品权重" prop="chance">
        <el-input-number
          v-model="formValidate.chance"
          placeholder="请输入奖品权重"
          :max="100"
          :min="1"
          :precision="0"
          style="width: 300px"
        ></el-input-number>
      </el-form-item>
      <el-form-item label="提示语" prop="prompt">
        <el-input
          v-model="formValidate.prompt"
          :maxlength="15"
          placeholder="请输入提示语"
          style="width: 300px"
        ></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="handleSubmit('formValidate')"
          >提交</el-button
        >
      </el-form-item>
    </el-form>

    <el-dialog
      :visible.sync="modals"
      title="商品列表"
      width="60%"
      :modal="false"
      :close-on-click-modal="false"
    >
      <goods-list
        ref="goodslist"
        v-if="modals"
        @getProductId="getProductId"
      ></goods-list>
    </el-dialog>

    <!-- 新增数据处理 -->

    <el-dialog
      :visible.sync="showmodel"
      :close-on-click-modal="false"
      title="优惠券列表"
      width="60%"
      :modal="false"
    >
      <coupon-list
        ref="couponTemplates"
        v-if="showmodel"
        :luckDraw="true"
        @getCouponId="getCouponId"
      ></coupon-list>
    </el-dialog>

    <el-dialog
      :visible.sync="showmodel_mer"
      :close-on-click-modal="false"
      title="商家优惠券列表"
      width="60%"
      :modal="false"
    >
      <coupon-list-mer
        ref="couponTemplates"
        v-if="showmodel_mer"
        :luckDraw="true"
        @getCouponId="getCouponId_mer"
      ></coupon-list-mer>
    </el-dialog>

    <!--<coupon-list-->
    <!--ref="couponTemplates"-->
    <!--@nameId="nameId"-->
    <!--:updateIds="updateIds"-->
    <!--:updateName="updateName"-->
    <!--&gt;</coupon-list>-->
  </div>
</template>

<script>
import couponList from "@/components/couponList";
import couponListMer from "@/components/couponListMer";
import uploadPicture from "@/components/uploadPicture";
import goodsList from "@/components/goodsList/index";
// import freightTemplate from '@/components/freightTemplate';
export default {
  // components: { uploadPictures, goodsList, freightTemplate, couponList },
  components: { goodsList, uploadPicture, couponList, couponListMer },
  data() {
    return {
      showmodel: false,
      showmodel_mer: false,
      modalPic: false,
      modals: false,
      isChoice: "单选",
      updateIds: [],
      updateName: [],
      goodsData: {
        pic: "",
        product_id: "",
        img: "",
        coverImg: "",
      },
      formValidate: {
        type: 5, //类型 1：未中奖2：积分  3:余额  4：红包 5:优惠券 6：站内商品
        name: "", //活动名称
        num: 0, //奖品数量
        image: "", //奖品图片
        chance: 1, //中奖权重
        product_id: 0, //商品id
        coupon_id: 0, //优惠券id
        total: 0, //奖品数量
        prompt: "", //提示语
        goods_image: "", //自用商品图
        coupon_title: "", //优惠券名称
      },
      ruleValidate: {
        name: [
          {
            required: true,
            message: "商品名称",
            trigger: "blur",
          },
        ],
        goods_image: [
          {
            required: true,
            message: "请添加商品",
            trigger: "blur",
          },
        ],
        num: [
          {
            required: true,
            type: "number",
            message: "请输入金额数量",
            trigger: "blur",
          },
        ],
        chance: [
          {
            required: true,
            type: "number",
            message: "请输入商品权重",
            trigger: "blur",
          },
        ],
        image: [
          {
            required: true,
            message: "请选择奖品图片",
            trigger: "blur",
          },
        ],
        prompt: [
          {
            required: true,
            message: "请输入提示语",
            trigger: "blur",
          },
        ],
      },
      couponName: [],
      couponName_mer: [],
    };
  },
  props: {
    editData: {
      type: Object,
      default: () => {},
    },
  },
  watch: {
    editData(data) {},
  },
  mounted() {
    let keys = Object.keys(this.editData);
    console.log("keys的值", keys);
    keys.forEach((item) => {
      this.formValidate[item] = this.editData[item];
      if (
        item === "coupon_title" &&
        this.editData[item] &&
        this.editData["type"] == 5
      ) {
        this.couponName.push({
          title: this.editData[item],
          coupon_id: this.editData.coupon_id,
        });
      }
      if (
        item === "coupon_title" &&
        this.editData[item] &&
        this.editData["type"] == 10
      ) {
        /*添加couponName_mer的数据处理*/
        this.couponName_mer.push({
          title: this.editData[item],
          coupon_id: this.editData.coupon_id,
        });
        console.log("this.couponName", this.couponName);
      }
    });
  },
  methods: {
    getCouponId(e) {
      this.formValidate.coupon_id = e.coupon_id;
      this.formValidate.coupon_title = e.title;
      this.formValidate.name = e.title;
      let couponName = [];
      couponName.push(e);
      this.couponName = couponName;
      this.showmodel = false;
    },
    getCouponId_mer(e) {
      this.formValidate.coupon_id = e.coupon_id;
      this.formValidate.coupon_title = e.title;
      this.formValidate.name = e.title;
      let couponName_mer = [];
      couponName_mer.push(e);
      this.couponName_mer = couponName_mer;
      this.showmodel_mer = false;
    },
    handleSubmit(name) {
      this.$refs[name].validate((valid) => {
        if (valid) {
          this.$emit("addGoodsData", this.formValidate);
          this.$message.success("添加成功");
        } else {
          this.$message.warning("请完善数据");
        }
      });
    },
    addgoods() {
      this.modals = true;
    },

    // 获取单张图片信息
    getPic(pc) {
      this.formValidate.image = pc.att_dir;
      this.modalPic = false;
    },
    // 点击商品图
    modalPicTap() {
      const _this = this;
      this.$modalUpload(function (img) {
        _this.formValidate.image = img[0];
      }, 1);
    },
    cancel() {
      this.modals = false;
      this.showmodel = false;
    },
    // 选择的商品
    getProductId(productList) {
      // if (productList.length > 1) {
      //   this.$Message.warning("最多添加一个商品");
      //   return;
      // }
      this.formValidate.product_id = productList.product_id;
      this.formValidate.goods_image = productList.image;

      this.formValidate.name = productList.store_name;
      this.modals = false;
      // productList.forEach((value) => {
      //   this.formValidate.product_id = value.product_id;
      //   this.formValidate.goods_image = value.image;
      // });
    },
    removeGoods() {
      this.formValidate.product_id = "";
      this.formValidate.goods_image = "";
    },
    remove() {
      this.formValidate.image = "";
    },
    // 添加优惠券
    addCoupon() {
      this.showmodel = true;
      //this.$refs.couponTemplates.tableList();
    },
    // 添加优惠券
    addCoupon_mer() {
      this.showmodel_mer = true;
      //this.$refs.couponTemplates.tableList();
    },
    handleClose(name) {
      this.couponName.splice(0, 1);
      this.formValidate.coupon_id = 0;
    },
    handleClose_mer(name) {
      this.couponName_mer.splice(0, 1);
      this.formValidate.coupon_id = 0;
    },
    //对象数组去重；
    unique(arr) {
      const res = new Map();
      return arr.filter((arr) => !res.has(arr.id) && res.set(arr.id, 1));
    },
  },
};
</script>

<style scoped lang="scss">
.pictrueBox {
  display: inline-block;
}

.pictrue {
  width: 60px;
  height: 60px;
  border: 1px dotted rgba(0, 0, 0, 0.1);
  margin-right: 15px;
  display: inline-block;
  position: relative;
  cursor: pointer;

  img {
    width: 100%;
    height: 100%;
  }

  .btndel {
    position: absolute;
    z-index: 1;
    width: 20px !important;
    height: 20px !important;
    left: 46px;
    top: -4px;
  }
}

.upload-list {
  width: 58px;
  height: 58px;
  line-height: 58px;
  border: 1px dotted rgba(0, 0, 0, 0.1);
  border-radius: 4px;
  background: rgba(0, 0, 0, 0.02);
  cursor: pointer;
  position: relative;
}

.upload-list img {
  display: block;
  width: 100%;
  height: 100%;
}

.upLoad {
  width: 58px;
  height: 58px;
  line-height: 58px;
  border: 1px dotted rgba(0, 0, 0, 0.1);
  border-radius: 4px;
  background: rgba(0, 0, 0, 0.02);
  cursor: pointer;
}

.ivu-icon-ios-close-circle {
  position: absolute;
  top: 0;
  right: 0;
  transform: translate(50%, -50%);
}

.grey {
  color: #999;
}

.elicon {
  position: absolute;
  right: 0;
  bottom: 0;
  font-size: 20px;
}

.addicon {
  font-size: 30px;
}
</style>
